package ca.etsmtl.log730.lab3.controllers;

import java.util.ArrayList;
import java.util.List;

import ca.etsmtl.log730.lab3.application.EventBusConnector;
import ca.etsmtl.log730.lab3.controllers.branch.BranchesServerThread;
import ca.etsmtl.log730.lab3.model.Branch;

/*
 * JE SUIS LE CONTROLLEUR DE LA SUCCURSALE
 * JE CRÉÉ ET JE SYNCHRONIZE L'ULTIME INSTANCE DE LA-DITE SUCCURSALE
 */
public class BranchController {
	private Branch branch = new Branch(String.valueOf((int)Math.random() * 10) , 2100);

	private String ipAddress = "127.0.0.1";
	private int centralBankPort = 12045;
	private int branchBankPort = 1234;
	
	private List<Branch> branchesList = new ArrayList<Branch>();
	
	public int getBranchBankPort() {
		return branchBankPort;
	}

	public void setBranchBankPort(int branchBankPort) {
		this.branchBankPort = branchBankPort;
	}

	private EventBusConnector mainConnector;
	private BranchesServerThread branchesConnector;
	
	public BranchesServerThread getBranchesConnector() {
		return branchesConnector;
	}

	public void setBranchesConnector(BranchesServerThread branchesConnector) {
		this.branchesConnector = branchesConnector;
	}

	private List<Branch> branchList = new ArrayList<Branch>();
	public Branch getBranch() {
		return branch;
	}

	public void setBranch(Branch branch) {
		this.branch = branch;
	}

	public EventBusConnector getMainConnector() {
		return mainConnector;
	}

	public void setMainConnector(EventBusConnector connector) {
		this.mainConnector = connector;
	}

	public List<Branch> getBranchList() {
		return branchList;
	}

	public void setBranchList(List<Branch> branchList) {
		this.branchList = branchList;
	}
	
	public void open() {
		setMainConnector(new EventBusConnector(new ArrayList<Class>(), ipAddress, centralBankPort));
		setBranchesConnector(new BranchesServerThread(getBranchBankPort()));
		getMainConnector().start();
		getBranchesConnector().start();
	}

	public List<Branch> getBranchesList() {
		return branchesList;
	}

	public void setBranchesList(List<Branch> branchesList) {
		this.branchesList = branchesList;
	}
}
